﻿@*
    // Copyright (c) Alex Reich.
    // Licensed under the CC BY 4.0 License.
*@
@page "/demo/{WorkflowName?}"
@using Newtonsoft.Json
@using RulesEngine.Models
@using RulesEngineEditor.Models
@inject HttpClient Http

<RulesEngineEditorPage EditorRulesEngine="re" @bind-Workflows="Workflows" InputJSON="@Inputs" CurrentWorkflowName="@WorkflowName" />

@code {
    [Parameter]
    public string WorkflowName { get; set; }
    private RulesEngine.RulesEngine re;

    private Workflow[] _workflows;
    private Workflow[] Workflows { get { return _workflows; } set { _workflows = value; } }

    private string Inputs;

    enum FooEnum
    {
        bar = 123
    }

    protected override async Task OnInitializedAsync()
    {
        var settings = new ReSettings
        {
            CustomTypes = new Type[] { typeof(FooEnum) }
        };
        re = new RulesEngine.RulesEngine(new string[0], settings);

        var file = await Http.GetAsync("sample-data/discount.json");
        var fileData = await file.Content.ReadAsStringAsync();
        var workflow = JsonConvert.DeserializeObject<List<Workflow>>(fileData);
        Workflows = workflow.ToArray();

        file = await Http.GetAsync("sample-data/discountInputs.json");
        Inputs = await file.Content.ReadAsStringAsync();
    }
}